<template >
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.hoscode" placeholder="医院编号" />
      </el-form-item>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="searchData()"
        :loading="listLoading"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 工具条 -->
    <div style="margin-bottom: 5px">
      <el-button type="danger" size="mini" @click="removeRows()"
        >批量删除</el-button
      >
    </div>

    <!-- 医院设置列表 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      >
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="hosname" label="医院名称" width="150" />
      <el-table-column prop="hoscode" label="医院编号" width="100" />
      <el-table-column prop="apiUrl" label="地址" width="200" />
      <el-table-column prop="contactsName" label="联系人" />
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="260" align="center">
        <template slot-scope="scope">
          <router-link :to="'/syt/hospset/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button
            >
          </router-link>
          <el-button
            style="margin-left: 5px"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="lockHostSet(scope.row.id, 0)"
            >锁定</el-button
          >
          <el-button
            v-if="scope.row.status == 0"
            type="success"
            size="mini"
            icon="el-icon-delete"
            @click="lockHostSet(scope.row.id, 1)"
            >解锁</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[2, 5, 10]"
      style="padding: 30px 0; text-align: center"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>
<script>
import hospsetApi from "@/api/syt/hospset";
export default {
  data() {
    return {
      listLoading: true, // 是否显示loading信息
      list: [], // 数据列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询条件
      multipleSelection: [], // 批量选择中选择的记录列表
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    //表格查询
    fetchData() {
      hospsetApi
        .PageList(this.page, this.limit, this.searchObj)
        .then((response) => {
          console.log(response);
          this.list = response.data.records;
          this.total = response.data.total;
          this.listLoading = false;
        });
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.limit = size;
      this.fetchData();
    },
    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.fetchData();
    },

    //查询数据
    searchData() {
      this.page = 1;
      this.fetchData();
    },
    //清空查询
    resetData() {
      this.page = 1; //重置页码
      this.searchObj = {}; //清空查询条件
      this.fetchData(); //重置列表数据
    },
    //根据ID删除医院设置
    removeDataById(id) {
      // debugger
      // console.log(memberId)
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          hospsetApi.removeById(id).then((response) => {
            this.fetchData();
            this.$message.success(response.message);
          });
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },

    // 当表格复选框选项发生变化的时候触发
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    //批量删除
    removeRows() {
      if (this.multipleSelection.length == 0) {
        this.$message.warning("请选择要删除的记录");
        return;
      }

      this.$confirm("此操作将永久删除医院设置信息, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          //确定执行then方法
          var idList = [];
          this.multipleSelection.forEach((item) => {
            idList.push(item.id);
          });
          //调用接口
          hospsetApi.removeRows(idList).then((response) => {
            //提示
            this.$message.success(response.message),
              //刷新页面
              this.fetchData();
          });
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },

    //锁定和取消锁定
    lockHostSet(id, status) {
      hospsetApi.lockHospSet(id, status).then((response) => {
        //刷新
        this.fetchData();
      });
    },
  },
};
</script>

